﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Joyintech.DBUtility;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using Joyintech.IDAL.inpiao365;

namespace Joyintech.SQL2008DAL.inpiao365
{
    public class pageSplit_DAL_Custom : AbstractDAL,IpageSplit_DAL
    {
        /// <summary>
        /// 获取指定栏目的分页数据项
        /// </summary>
        /// <param name="strSql">查询语句,语句尾部不可带有order by</param>
        /// <param name="orderby">如：AddDate desc</param>
        /// <param name="StartRowNum">记录开始索引</param>
        /// <param name="EndRowNum">记录结束索引</param>
        /// <returns></returns>
        public DataSet GetPagerList(string strSelect,string orderby, int StartRowNum, int EndRowNum)
        {
            string strSql = "select * from (select *,ROW_NUMBER() OVER (ORDER BY " + orderby + ") AS RowNo from (" + strSelect + ") AS A) AS B " + "where RowNo >= " + StartRowNum + " and RowNo <= " + EndRowNum;

            Database db = this.CreateDatabase();
            return ExecuteDataSet(db, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获取查询结果
        /// </summary>
        /// <param name="strSql">查询语句</param>
        /// <returns></returns>
        public DataSet GetList(string strSql)
        {
            Database db = this.CreateDatabase();
            return ExecuteDataSet(db, CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 根据指定语句获取记录总数
        /// </summary>
        /// <param name="strSelectCount">如：select count(*) from systemNodeContent</param>
        /// <returns></returns>
        public int GetPagerTotalCount(string strSelectCount)
        {
            Database db = this.CreateDatabase();

            int cmdresult;

            DbCommand dbCommand = db.GetSqlStringCommand(strSelectCount.ToString());

            object obj = ExecuteScalar(db, dbCommand);

            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }

            return cmdresult;
        }
    }
}
